package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    //分页查询
    @GetMapping("/setmeal/page")
    public ResultInfo findByPage(
            String name,
            @RequestParam(value = "page",defaultValue ="1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize
    ){
        //调用service
        Page<Setmeal> page=setmealService.findByPage(name,pageNum,pageSize);
        return ResultInfo.success(page);
    }
    //新增套餐
    @PostMapping("/setmeal")
    public ResultInfo save(@RequestBody Setmeal setmeal){
        //调用service查询
        setmealService.save(setmeal);
        //返回结果
        return ResultInfo.success(null);
    }
    //删除套餐
    @DeleteMapping("/setmeal")
    public ResultInfo delete(Long[] ids){
        //调用service
        if (ids !=null && ids.length>0) {
            setmealService.delete(ids);
        }
        return ResultInfo.success(null);
    }
    //停售
    @PostMapping("/setmeal/status/{status}")
    public ResultInfo status(
            @PathVariable("status") Integer status,
            Long[] ids){
        //调用service
        if (ids!=null && ids.length>0){
            setmealService.status(status,ids);
        }
        return ResultInfo.success(null);
    }
    //回显
    @GetMapping("/setmeal/{id}")
    public ResultInfo findById(
            @PathVariable("id") Long id){
        Setmeal setmeal=setmealService.findById(id);
        return ResultInfo.success(setmeal);
    }
    //修改
    @PutMapping("/setmeal")
    public ResultInfo update(@RequestBody Setmeal setmeal){
        //调用service
        setmealService.update(setmeal);
        //返回结果
        return ResultInfo.success(null);
    }
}
